package hikari.math;

import org.swig.jirr.vector3df;

/**
 * Class for resolving cylinrical coordinates in local coordinate system
 */
public class CylCoordResolver {
    private vector3df center;
    private vector3df normal;
    private vector3df v1;
    private vector3df v2;
    private float zRotation=0;
    
    public CylCoordResolver(vector3df center, vector3df normal) {
        this(center, normal, 0);
    }
    
    public CylCoordResolver(vector3df center, vector3df normal, float zRotation) {
        this.zRotation=(float)Math.toRadians(zRotation);
        this.center = center;
        this.normal = new vector3df(normal).normalize();

        v1 = new vector3df(0, normal.getZ(), -normal.getY());
        v2 = new vector3df(normal.getZ(), 0, -normal.getX());
        if (!v1.equals(vector3df.NULLVECTOR)) {
            v1 = v1.dividedOperator((float) Math.sqrt(1 - (normal.getX() * normal.getX())));
            v2 = v1.crossProduct(normal);
        } else {
            v2 = v2.dividedOperator((float) Math.sqrt(1 - (normal.getY() * normal.getY())));
            v1 = v2.crossProduct(normal);
        }
    }

    /**
     * 
     * @param angle
     * angle in radians
     * @param radius
     * radius from start of the local coordinate plane
     * @param height
     * the height above the local coordinate plane
     * @return 
     */
    public vector3df resolve(float angle, float radius, float height) {
        angle+=zRotation;
        vector3df vector = center.addOperator(v1.timesOperator((float) (radius * Math.cos(angle)))).addOperator(v2.timesOperator((float) (radius * Math.sin(angle))));
        vector.assignPlusOperator(normal.timesOperator(height));
        return vector;
    }
}
